/*
 * Copyright 2001-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.training.killerapp.store.memory;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.training.killerapp.KillerAppDataStore;
import com.training.killerapp.model.Page;
import com.training.killerapp.model.User;

/**
 * The Killer App data store backed by a simple Map.
 * 
 * @since 1.0
 */
public class KillerAppMemoryStore
    implements KillerAppDataStore
{
    protected Map pageStore;
    protected Map userStore;
    protected int id;

    /**
     * Constructs a java.util.Map which stores the pages by generated IDs.
     */
    public KillerAppMemoryStore()
    {
        pageStore = new TreeMap();
        id = 0;

        userStore = new HashMap();
    }

    public int createPage( Page page, String ownerEmail ) 
    {
        if( ownerEmail == null )
        {
            throw new NullPointerException( "The User email must be set to create a Page." );
        }

        Integer sid = Integer.valueOf( ++id );

        page.setId( sid.intValue() );

        pageStore.put( sid, page );

        return page.getId();
    }

    public String createUser( User user )
    {
        if( user.getEmail() == null )
        {
            throw new NullPointerException( "The User email must be set to update a User." );
        }

        userStore.put( user.getEmail(), user );

        return user.getEmail();
    }

    public void deleteUser( User user )
    {
        if( user.getPage() != null )
        {
            pageStore.remove( Integer.valueOf( user.getPage().getId() ) );
        }
        userStore.remove( user.getEmail() );
    }

    public Page getPage( int page )
    {
        return (Page)pageStore.get( Integer.valueOf( page ) );
    }

    public User getUser( String email )
    {
        return (User)userStore.get( email );
    }

    public Collection getUserCollection()
    {
        return userStore.values();
    }

    public void updatePage( Page page )
    {
        Page storedPage = (Page)pageStore.get( Integer.valueOf( page.getId() ) );

        page.setOwner( storedPage.getOwner() );

        pageStore.put( Integer.valueOf( page.getId() ), page );
    }

    public void updateUser( User user )
    {
        if( user.getEmail() == null )
        {
            throw new NullPointerException( "The User email must be set to update a User." );
        }

        userStore.put( user.getEmail(), user );

        // ensure the user objects are ==
        Page page = (Page)pageStore.get( Integer.valueOf( user.getPage().getId() ) );
        page.setOwner( user );
    }
}
